/*
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/mit-license.php
 */

package ru.ssau.berlin.entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.*;

/**
 *
 * @author Caesar
 */
@Entity
@NamedQueries({
    @NamedQuery(
       name = "findPersonByName",
       query = "SELECT object(p) FROM Person as p WHERE p.name LIKE :personName"
    ),
    @NamedQuery(
        name = "findAllPersons",
        query = "SELECT object(p) FROM Person as p"
    )
})
public class Person implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @ManyToOne
    private PersonType type;

    @OneToMany(mappedBy = "person")
    private List<PersonParameter> parameters;

    @ManyToMany()
    private List<PersonGroup> groups;

    @ManyToMany()
    private List<Event> events;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public PersonType getType() {
        return type;
    }

    public void setType(PersonType type) {
        this.type = type;
    }

    public List<PersonParameter> getParameters() {
        return parameters;
    }

    public List<PersonGroup> getGroups() {
        return groups;
    }

    public List<Event> getEvents() {
        return events;
    }

    @PostLoad
    public void postLoad() {
        parameters.size();
        groups.size();
        events.size();
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Person)) {
            return false;
        }
        Person other = (Person) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "ru.ssau.berlin.entity.Person[id=" + id + "]";
    }

}
